From 7e62eca7a19e2c2e9c8e64af318750415636c1b8 Mon Sep 17 00:00:00 2001 From: Keir Fraser Date: Wed, 19 May 2010 08:20:46 +0100 Subject: [PATCH] libxl: Compilation and other small fixes * Some of the library functions such as fscanf, system, and asprintf are declared with warn_unused_result (ubuntu server 9.10), causing compilation errors in libxl. * When using asprintf, the caller is responsible for freeing the memory. * memset takes wrong size argument in one of the places (caught by a builtin gcc check). Signed-off-by: Dulloor Rao --- tools/libxl/libxl.c | 15 ++++++------ tools/libxl/xl_cmdimpl.c | 53 +++++++++++++++++++++++++++------------- 2 files changed, 44 insertions(+), 24 deletions(-) diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c index 2fc16232cd..7da3695ff8 100644 --- a/tools/libxl/libxl.c +++ b/tools/libxl/libxl.c @@ -1262,7 +1262,8 @@ static char *get_blktap2_device(struct libxl_ctx *ctx, char *name, char *type) while (!feof(f)) { - fscanf(f, "%d %s", &devnum, buf); + if (fscanf(f, "%d %s", &devnum, buf) != 2) + continue; p = strchr(buf, ':'); if (p == NULL) continue; @@ -2276,7 +2277,8 @@ int libxl_device_pci_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci return -1; } for (i = 0; i < PROC_PCI_NUM_RESOURCES; i++) { - fscanf(f, "0x%x 0x%x 0x%x", &start, &end, &flags); + if (fscanf(f, "0x%x 0x%x 0x%x", &start, &end, &flags) != 3) + continue; size = end - start + 1; if (start) { if (flags & PCI_BAR_IO) { @@ -2299,8 +2301,7 @@ int libxl_device_pci_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path); goto out; } - fscanf(f, "%u", &irq); - if (irq) { + if ((fscanf(f, "%u", &irq) == 1) && irq) { rc = xc_physdev_map_pirq(ctx->xch, domid, irq, &irq); if (rc < 0) { XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: xc_physdev_map_pirq irq=%d", irq); @@ -2364,7 +2365,8 @@ int libxl_device_pci_remove(struct libxl_ctx *ctx, uint32_t domid, libxl_device_ goto skip1; } for (i = 0; i < PROC_PCI_NUM_RESOURCES; i++) { - fscanf(f, "0x%x 0x%x 0x%x\n", &start, &end, &flags); + if (fscanf(f, "0x%x 0x%x 0x%x\n", &start, &end, &flags) != 3) + continue; size = end - start + 1; if (start) { if (flags & PCI_BAR_IO) { @@ -2388,8 +2390,7 @@ skip1: XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path); goto out; } - fscanf(f, "%u", &irq); - if (irq) { + if ((fscanf(f, "%u", &irq) == 1) && irq) { rc = xc_physdev_unmap_pirq(ctx->xch, domid, irq); if (rc < 0) { XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_physdev_map_pirq irq=%d", irq); diff --git a/tools/libxl/xl_cmdimpl.c b/tools/libxl/xl_cmdimpl.c index 768547e4dd..b1fe305882 100644 --- a/tools/libxl/xl_cmdimpl.c +++ b/tools/libxl/xl_cmdimpl.c @@ -1141,7 +1141,7 @@ start: xc_domaininfo_t info; libxl_event event; libxl_device_disk disk; - memset(&info, 0x00, sizeof(xc_dominfo_t)); + memset(&info, 0x00, sizeof(xc_domaininfo_t)); FD_ZERO(&rfds); FD_SET(fd, &rfds); @@ -3227,9 +3227,7 @@ int main_top(int argc, char **argv) } } - system("xentop"); - - exit(0); + exit(system("xentop")); } int main_networkattach(int argc, char **argv) @@ -3560,6 +3558,7 @@ static char *uptime_to_string(unsigned long time, int short_mode) { int sec, min, hour, day; char *time_string; + int ret; day = (int)(time / 86400); time -= (day * 86400); @@ -3571,19 +3570,21 @@ static char *uptime_to_string(unsigned long time, int short_mode) if (short_mode) if (day > 1) - asprintf(&time_string, "%d days, %2d:%02d", day, hour, min); + ret = asprintf(&time_string, "%d days, %2d:%02d", day, hour, min); else if (day == 1) - asprintf(&time_string, "%d day, %2d:%02d", day, hour, min); + ret = asprintf(&time_string, "%d day, %2d:%02d", day, hour, min); else - asprintf(&time_string, "%2d:%02d", hour, min); + ret = asprintf(&time_string, "%2d:%02d", hour, min); else if (day > 1) - asprintf(&time_string, "%d days, %2d:%02d:%02d", day, hour, min, sec); + ret = asprintf(&time_string, "%d days, %2d:%02d:%02d", day, hour, min, sec); else if (day == 1) - asprintf(&time_string, "%d day, %2d:%02d:%02d", day, hour, min, sec); + ret = asprintf(&time_string, "%d day, %2d:%02d:%02d", day, hour, min, sec); else - asprintf(&time_string, "%2d:%02d:%02d", hour, min, sec); + ret = asprintf(&time_string, "%2d:%02d:%02d", hour, min, sec); + if (ret < 0) + return NULL; return time_string; } @@ -3609,8 +3610,9 @@ static void print_dom0_uptime(int short_mode, time_t now) int fd; char buf[512]; uint32_t uptime = 0; + char *uptime_str = 0; - fd = open("/proc/uptime", 'r'); + fd = open("/proc/uptime", O_RDONLY); if (fd == -1) goto err; @@ -3624,12 +3626,20 @@ static void print_dom0_uptime(int short_mode, time_t now) uptime = strtoul(buf, NULL, 10); if (short_mode) + { + uptime_str = uptime_to_string(uptime, 1); printf(" %s up %s, %s (%d)\n", current_time_to_string(now), - uptime_to_string(uptime, 1), libxl_domid_to_name(&ctx, 0), 0); + uptime_str, libxl_domid_to_name(&ctx, 0), 0); + } else + { + uptime_str = uptime_to_string(uptime, 0); printf("%-33s %4d %s\n", libxl_domid_to_name(&ctx, 0), - 0, uptime_to_string(uptime, 0)); + 0, uptime_str); + } + if (uptime_str) + free(uptime_str); return; err: fprintf(stderr, "Can not get Dom0 uptime.\n"); @@ -3640,19 +3650,28 @@ static void print_domU_uptime(uint32_t domuid, int short_mode, time_t now) { uint32_t s_time = 0; uint32_t uptime = 0; + char *uptime_str = 0; s_time = libxl_vm_get_start_time(&ctx, domuid); if (s_time == -1) return; uptime = now - s_time; if (short_mode) + { + uptime_str = uptime_to_string(uptime, 1); printf(" %s up %s, %s (%d)\n", current_time_to_string(now), - uptime_to_string(uptime, 1), - libxl_domid_to_name(&ctx, domuid), - domuid); + uptime_str, libxl_domid_to_name(&ctx, domuid), domuid); + } else + { + uptime_str = uptime_to_string(uptime, 0); printf("%-33s %4d %s\n", libxl_domid_to_name(&ctx, domuid), - domuid, uptime_to_string(uptime, 0)); + domuid, uptime_str); + } + + if (uptime_str) + free(uptime_str); + return; } static void print_uptime(int short_mode, uint32_t doms[], int nb_doms) -- 2.30.2